#include "TcpClient.h"

TcpClient::TcpClient(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
	this->setWindowTitle("Tcp客户端");
	this->setWindowIcon(QIcon(":/images/images/Qicon.jpg"));

	this->socket = new QTcpSocket;
	
}

TcpClient::~TcpClient()
{}

void TcpClient::on_connectButton_clicked()
{
	QString strIP = ui.ipLineEdit->text();
	QString strPort = ui.portLineEdit->text();
	
	this->socket->connectToHost(QHostAddress(strIP), strPort.toShort());

	QObject::connect(socket, &QTcpSocket::connected, [this]()
	{
		QMessageBox::information(this, "连接提示", "连接服务器成功");
		this->hide();
		chat *c = new chat(socket);
		c->show();
	});

	QObject::connect(socket, &QTcpSocket::disconnected, [this]()
	{

		QMessageBox::warning(this, "连接提示", "连接异常");
	});
}

void TcpClient::on_cancelButton_clicked()
{
	this->close();
}